/**
 * 
 */
package swings.mycomponent.menu;

import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.UIManager;

import org.apache.log4j.Logger;

/**
 * @Title: MenuBar.java
 * @Description: TODO(创建 JmenuBar 组件，演示其用法 )
 * @author H819
 * @date 2011-3-13
 * @version V1.0
 */

public class MenuBar extends JMenuBar {

	private final ResourceBundle BUNDLE = ResourceBundle
			.getBundle("swings.mycomponent.mycomponent");

	private Logger log = Logger.getLogger(MenuBar.class);

	private Desktop desktop;

	private JFrame menuBarParentJframe;

	/**
	 * 
	 */
	public MenuBar(JFrame menuBarParent) {
		// TODO Auto-generated constructor stub

		menuBarParentJframe = menuBarParent;

		if (Desktop.isDesktopSupported()) {
			desktop = Desktop.getDesktop();
		} else {
			System.out.println("Desktop class is not supported");
			System.exit(1);
		}

		JMenu fileMenu = new JMenu(BUNDLE.getString("menu.file"));
		this.add(fileMenu);
		
		JMenuItem exitMenuItem = new JMenuItem(BUNDLE.getString("menu.file.exit")); //$NON-NLS-1$
		exitMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				System.exit(0);

			}
		});
		fileMenu.add(exitMenuItem);

		JMenu helpMenu = new JMenu(BUNDLE.getString("menu.help"));
		this.add(helpMenu);

		/**
		 * 创建 aboutMenu 菜单项
		 */

		JMenuItem helpMenuItem = new JMenuItem(
				BUNDLE.getString("menu.help.help"));

		helpMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				try {

					/*
					 * 打开本地文件
					 */
					// String uripath = MenuBar.class.getClassLoader()
					// .getResource("").getPath()
					// + "com/info/doc/help.html";
					// uripath = FilenameUtils.separatorsToSystem(uripath);
					// uripath = new File(uripath).toURI().toString();
					// log.info("open :" + uripath);

					URI browseURI = new URI(
							"http://www.canhelp.cn/dev/PDF%20Batch%20Decrypt.html");
					desktop.browse(browseURI);
				} catch (Exception ex) {
					System.out.println(ex.getMessage());
				}

			}
		});

		helpMenu.add(helpMenuItem);
		
		JMenuItem regMenuItem = new JMenuItem(BUNDLE.getString("menu.help.reg")); 
		regMenuItem.setEnabled(false);
		regMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});
		
		helpMenu.add(regMenuItem);

		JMenuItem aboutMenuItem = new JMenuItem(
				BUNDLE.getString("menu.help.about")); //$NON-NLS-1$
		aboutMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				log.info("aboutMenu was clicked...");

				JOptionPane.showMessageDialog(
						menuBarParentJframe,
						BUNDLE.getString("menu.help.about.info"),
						"About",
						JOptionPane.INFORMATION_MESSAGE,
						new ImageIcon(
								MenuBar.class
										.getResource("/swings/mycomponent/source/image/java-duke-logo_80x80.png")));

			}
		});

		JSeparator separator = new JSeparator();
		helpMenu.add(separator);
		helpMenu.add(aboutMenuItem);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// 测试
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Throwable e) {
			e.printStackTrace();
		}

		EventQueue.invokeLater(new Runnable() {

			public void run() {
				try {
					JFrame testFrame = new JFrame();
					testFrame.setBounds(100, 100, 450, 300);
					testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

					// 窗口居中
					Point center = GraphicsEnvironment
							.getLocalGraphicsEnvironment().getCenterPoint();
					int windowWidth = 450;
					int windowHeight = 300;

					testFrame.setBounds(center.x - windowWidth / 2, center.y
							- windowHeight / 2, windowWidth, windowHeight);

					testFrame.setJMenuBar(new MenuBar(testFrame));
					testFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

}
